//
//  LoginViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/12/30.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "LoginViewController.h"
#import "JMJLoginTextField.h"
#import "RegisterViewController.h"

#define kTextFieldTopMargin 85 / 2
#define kTextFieldLeftMargin 85 / 2
#define kTextFieldHeight 40
#define kTextFieldMidMargin 22 /2
#define kButtonWidth 100
#define kButtonHeight 30

@interface LoginViewController () <UITextFieldDelegate>

@end

@implementation LoginViewController

{
    JMJLoginTextField *_userNameTextField;
    JMJLoginTextField *_passwordTextField;
    UIButton *_button;
    UILabel *_registLabel;
    UILabel *_forgetLabel;
}

#pragma mark - View Controller Life Cycle

- (instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self customNavBar];
    
    [self customMainView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customNavBar{
    self.title = @"金马甲账号登录";
    
    UIBarButtonItem *bbi1 = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonClick)];
    self.navigationItem.leftBarButtonItem = bbi1;
    
    UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc] initWithTitle:@"帮助" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonClick)];
    self.navigationItem.rightBarButtonItem = bbi2;
}


- (void)customMainView{
    
    _userNameTextField = [[JMJLoginTextField alloc] initWithFrame:CGRectMake(kTextFieldLeftMargin, kNavigationBarHeight + kTextFieldTopMargin, UISCREENWIDTH - 2 * kTextFieldLeftMargin, kTextFieldHeight)];
    _passwordTextField = [[JMJLoginTextField alloc] initWithFrame:CGRectMake(kTextFieldLeftMargin, _userNameTextField.bottom + kTextFieldMidMargin, _userNameTextField.width, kTextFieldHeight)];
    _userNameTextField.layer.borderColor = [RGB_COLOR(@"#ff7400") CGColor];
    _passwordTextField.layer.borderColor = [RGB_COLOR(@"#ff7400") CGColor];
    _userNameTextField.layer.borderWidth = 2;
    _passwordTextField.layer.borderWidth = 2;
    _userNameTextField.layer.cornerRadius = 5;
    _passwordTextField.layer.cornerRadius = 5;
    _userNameTextField.textColor = RGB_COLOR(@"#ff7400");
    _passwordTextField.textColor = RGB_COLOR(@"#ff7400");
    
    UIImageView *userIcon = [[UIImageView alloc] init];
    userIcon.image = [UIImage imageNamed:@"用户"];
    userIcon.contentMode = UIViewContentModeCenter;
    _userNameTextField.leftView = userIcon;
    _userNameTextField.leftViewMode = UITextFieldViewModeAlways;
    
    UIButton *userListIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    [userListIcon setImage:[UIImage imageNamed:@"显示记录1"] forState:UIControlStateNormal];
    [userListIcon setImage:[UIImage imageNamed:@"显示记录"] forState:UIControlStateSelected];
    userListIcon.tag = 1;
    [userListIcon addTarget:self action:@selector(showUserList:) forControlEvents:UIControlEventTouchUpInside];
    _userNameTextField.rightView = userListIcon;
    _userNameTextField.rightViewMode = UITextFieldViewModeAlways;
    
    
    UIImageView *passIcon = [[UIImageView alloc] init];
    passIcon.image = [UIImage imageNamed:@"密码"];
    passIcon.contentMode = UIViewContentModeCenter;
    _passwordTextField.leftView = passIcon;
    _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    
    UIButton *passwordShowIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    [passwordShowIcon setImage:[UIImage imageNamed:@"密码可视1"] forState:UIControlStateNormal];
    [passwordShowIcon setImage:[UIImage imageNamed:@"密码可视"] forState:UIControlStateSelected];
    passwordShowIcon.tag = 2;
    [passwordShowIcon addTarget:self action:@selector(showUserList:) forControlEvents:UIControlEventTouchUpInside];
    _passwordTextField.rightView = passwordShowIcon;
    _passwordTextField.rightViewMode = UITextFieldViewModeAlways;
    _passwordTextField.secureTextEntry = YES;
    
    _userNameTextField.placeholder = @"请输入会员名";
    _passwordTextField.placeholder = @"请输入密码";
    
    _userNameTextField.delegate = self;
    _passwordTextField.delegate = self;
    
    [self.view addSubview:_userNameTextField];
    [self.view addSubview:_passwordTextField];
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(_userNameTextField.left, _passwordTextField.bottom + kTextFieldTopMargin, _userNameTextField.width, kTextFieldHeight);
    [_button setTitle:@"登录" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [_button setBackgroundColor:RGB_COLOR(@"#d91f00")];
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _button.opaque = 0.2;
    _button.layer.cornerRadius = 5;
    [self.view addSubview:_button];
    
    _registLabel = [[UILabel alloc] initWithFrame:CGRectMake(_userNameTextField.left, _button.bottom + kTextFieldMidMargin, kButtonWidth, kButtonHeight)];
    _registLabel.text = @"立即注册";
    _registLabel.textAlignment = NSTextAlignmentLeft;
    _registLabel.textColor = RGB_COLOR(@"#d91f00");
    _registLabel.font = [UIFont systemFontOfSize:11];
    _registLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *registerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(registerNewAccont)];
    [_registLabel addGestureRecognizer:registerTap];
    [self.view addSubview:_registLabel];
    
    _forgetLabel = [[UILabel alloc] initWithFrame:CGRectMake(_userNameTextField.right - kButtonWidth, _button.bottom + kTextFieldMidMargin, kButtonWidth, kButtonHeight)];
    _forgetLabel.text = @"忘记账号";
    _forgetLabel.textAlignment = NSTextAlignmentRight;
    _forgetLabel.textColor = RGB_COLOR(@"#8b8b8b");
    _forgetLabel.font = [UIFont systemFontOfSize:11];
    _forgetLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *forgetTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(forgetAccount)];
    [_forgetLabel addGestureRecognizer:forgetTap];
    [self.view addSubview:_forgetLabel];
}


#pragma mark - Actions

- (void)btnClick{
    NSLog(@"%ld", self.selectedIndex);
    [self.navigationController popToRootViewControllerAnimated:YES];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"login"];
    self.navigationController.tabBarController.selectedIndex = self.selectedIndex;
}

- (void)leftBarButtonClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)rightBarButtonClick{
    NSLog(@"帮助");
}

- (void)showUserList:(UIButton *)btn{
    
    [_userNameTextField resignFirstResponder];
    [_passwordTextField resignFirstResponder];
    
    btn.selected = !btn.selected;
    if (btn.tag == 2) {
        _passwordTextField.secureTextEntry = !_passwordTextField.secureTextEntry;
    }else if(btn.tag == 1){
        
        
        
        float yOffSet = 300;
        
        CGRect rect = _passwordTextField.frame;
        CGRect rect2 = _button.frame;
        CGRect rect3 = _registLabel.frame;
        CGRect rect4 = _forgetLabel.frame;
        
        if (btn.selected == YES) {
            _passwordTextField.frame = CGRectMake(rect.origin.x, rect.origin.y + yOffSet, rect.size.width, rect.size.height);
            _button.frame = CGRectMake(rect2.origin.x, rect2.origin.y + yOffSet, rect2.size.width, rect2.size.height);
            _registLabel.frame = CGRectMake(rect3.origin.x, rect3.origin.y + yOffSet, rect3.size.width, rect3.size.height);
            _forgetLabel.frame = CGRectMake(rect4.origin.x, rect4.origin.y + yOffSet, rect4.size.width, rect4.size.height);
        }else{
            _passwordTextField.frame = CGRectMake(rect.origin.x, rect.origin.y - yOffSet, rect.size.width, rect.size.height);
            _button.frame = CGRectMake(rect2.origin.x, rect2.origin.y - yOffSet, rect2.size.width, rect2.size.height);
            _registLabel.frame = CGRectMake(rect3.origin.x, rect3.origin.y - yOffSet, rect3.size.width, rect3.size.height);
            _forgetLabel.frame = CGRectMake(rect4.origin.x, rect4.origin.y - yOffSet, rect4.size.width, rect4.size.height);
        }
        
       
    }
}

- (void)registerNewAccont{
    NSLog(@"wow");
    RegisterViewController *rvc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:rvc animated:NO];
}

- (void)forgetAccount{
    NSLog(@"Oh no!");
}

#pragma mark - UITextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
